//
//  YAYIEditNameViewController.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIEditNameViewController.h"
#import "YAYIUserinfoModel.h"

@interface YAYIEditNameViewController ()

@property (nonatomic, weak) UITextField *editTF;

@end

@implementation YAYIEditNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
    //
    [self setupNav];
    
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    [self setupSubView];
}


- (void)setupNav
{
    // left button
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    leftBtn.bounds = CGRectMake(0, 0, 40, 20);
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    // left button
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    rightBtn.bounds = CGRectMake(0, 0, 40, 20);
    [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}


- (void)setupSubView
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(SCREEN_width);
        make.height.mas_equalTo(44);
    }];
    
    UITextField *textFiled = [[UITextField alloc] init];
    textFiled.clearButtonMode = UITextFieldViewModeAlways;
    [bgView addSubview:textFiled];
    self.editTF = textFiled;
    [textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.bottom.mas_equalTo(bgView);
    }];
    
    if (self.modelItem.fullname) {
        textFiled.text = self.modelItem.fullname;
    }
}


#pragma mark 按钮相应方法
- (void)saveClick:(UIButton *)sender
{
    [self.editTF resignFirstResponder];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = [NSString stringWithFormat:@"%ld", self.modelItem.id];
    param[@"fullname"] = self.editTF.text;
    
    YAYILog(@"parm:%@", param);
    [YAYINetWorking postJsonSerilizeWithURL:me_editUserinfo param:param finish:^(NSData *data, NSDictionary *dict, NSError *error,NSDictionary *responseError) {
        if (error == nil) {
            YAYILog(@"保存标签：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                [SVProgressHUD showWithStatus:@"保存失败"];
                
            }else{
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self backAction];
                    // 执行代理刷新上级控制器
                    if ([self.delegate respondsToSelector:@selector(editNameDidChanged:)]) {
                        
                        [self.delegate editNameDidChanged:self.editTF.text];
                    }
                    
                });
            }
        }else{
            NSString *message = responseError[@"message"];
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}



@end
